package com.muqi.app.pj.shops;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.flyco.banner.anim.select.RotateEnter;
import com.flyco.banner.anim.unselect.NoAnimExist;
import com.flyco.widget.BannerItem;
import com.muqi.app.network.http.GlideImageUtils;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.pj.shops.ShopDetialBean.Tupian;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.TimeMangerUtil;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;

import com.muqi.app.user.widget.CircularImageView;
import com.muqi.app.user.widget.SimpleImageBanner;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class FictiousShopDetialActivity extends BaseFragmentActivity {
	private ImageView mIv_avator;
	private TextView mTv_name;
	private TextView mTv_free;
	private TextView mTv_remian;
	private Button mBt_exchange;
	private WebView mWv_detail;
	private String shop_id;
	private CircularImageView mCir_avator;
	private TextView mTv_teacher_name;
	private TextView mTv_teacget_time;

	private FictitiousShopInfo info;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fictitious_shop_detial);
	}

	@Override
	protected void onInit() {
		mIv_avator = (ImageView) findViewById(R.id.praise_avatar);
		mTv_name = (TextView) findViewById(R.id.praise_name);
		mTv_free = (TextView) findViewById(R.id.praise_jifen);
		mTv_remian = (TextView) findViewById(R.id.shop_remain);
		mBt_exchange = (Button) findViewById(R.id.shop_exchange);
		mWv_detail = (WebView) findViewById(R.id.shop_detail);
		mCir_avator = (CircularImageView) findViewById(R.id.teacher_avatar);
		mTv_teacher_name = (TextView) findViewById(R.id.teacher_name);
		mTv_teacget_time = (TextView) findViewById(R.id.teacher_time);
		// 支持javascript
		mWv_detail.getSettings().setJavaScriptEnabled(true);

		mWv_detail.getSettings().setUseWideViewPort(true);
		// 自适应屏幕
		mWv_detail.getSettings().setLayoutAlgorithm(
				LayoutAlgorithm.SINGLE_COLUMN);
		mWv_detail.getSettings().setLoadWithOverviewMode(true);

		mBt_exchange.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (info != null) {
					if (info.num <= 0) {
						showToast("已经被兑换完了,换个其他商品兑换吧");
					} else {
						Intent intent = new Intent(mContext,
								BuyXuniGoodActivity.class);
						intent.putExtra(BuyXuniGoodActivity.GOODS_DETAILS_BEAN,
								info);
						startActivity(intent);
					}
				}

			}
		});

	}

	@Override
	protected void onData() {
		Intent intent = getIntent();
		if (intent != null) {
			shop_id = intent.getStringExtra("shop_id");
		}

		getXuniDetial();
		getShopWebDetial();

	}

	private void getXuniDetial() {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());
		sendMap.put("id", shop_id);
		String requestUrl = ParamsUtils.buildParams(
				NetWorkApi.SHOP_XUNI_GOODS_DETIAL, sendMap);
		MyAsyncHttp.get(mContext, requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {
				info = ResponseUtils.getXuniGoodsDetial(mContext, result);

				if (info != null) {
					mTv_free.setText(info.credits);
					mTv_remian.setText(info.num + "");
					mTv_teacget_time.setText(TimeMangerUtil
							.getChinesetimeView(info.create_time));
					if (info.teacherInfo != null) {
						mTv_teacher_name.setText(info.teacherInfo.name);
						if (info.teacherInfo.avatarInfo != null) {
							GlideImageUtils.setImageLoader(mContext,
									info.teacherInfo.avatarInfo.fileurl_02,
									mCir_avator, R.drawable.default_avator);
						}

					}
					if (info.prizeInfo != null) {
						mTv_name.setText(info.prizeInfo.title);
						if (info.prizeInfo.fileurl != null) {
							GlideImageUtils.setImageLoader(mContext,
									info.prizeInfo.fileurl.fileurl_02,
									mIv_avator, R.drawable.app_logo);
						}

					}

				}
			}
		});

	}

	/**
	 * 获得商品网页的详细介绍
	 * 
	 */
	private void getShopWebDetial() {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		String requestUrl = null;

		sendMap.put("id", shop_id);
		requestUrl = ParamsUtils.buildParams(NetWorkApi.SHOP_XUNI_DETAIL_API,
				sendMap);

		MyAsyncHttp.get(mContext, requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {
				if (resuleCode == RESULTCODE.SUCCESS) {

					JSONObject obj;
					try {
						obj = new JSONObject(result);
						if (obj != null) {
							String url = obj.getString("data");
							mWv_detail.loadUrl(url);
						}

					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

			}
		});

	}

	public void onBack(View v) {
		finish();
	}

}
